﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MOJsdk
{
    public class MojFoldersEnum
    {
        public static MojEnumVar<string> LanguagesFolder
        { get { return new MojEnumVar<string>("LanguagesFolder", StorageType.SystemVar); } }
        public static MojEnumVar<string> PluginsFolder
        { get { return new MojEnumVar<string>("PluginsFolder", StorageType.SystemVar); } }
        public static MojEnumVar<string> SkinsFolder
        { get { return new MojEnumVar<string>("SkinsFolder", StorageType.SystemVar); } }
        public static MojEnumVar<string> TemporaryFolder
        { get { return new MojEnumVar<string>("TemporaryFolder", StorageType.SystemVar); } }
        public static MojEnumVar<string> DatabaseFolder
        { get { return new MojEnumVar<string>("DatabaseFolder", StorageType.SystemVar); } }
        public static MojEnumVar<string> ImagesFolder
        { get { return new MojEnumVar<string>("ImagesFolder", StorageType.SystemVar); } }
        public static MojEnumVar<string> LogsFolder
        { get { return new MojEnumVar<string>("LogsFolder", StorageType.SystemVar); } }
        public static MojEnumVar<string> FiltersFolder
        { get { return new MojEnumVar<string>("FiltersFolder", StorageType.SystemVar); } }
    }
    public class MojEnviromentVarsEnum
    {
        public static MojEnumVar<string> LanguageFile
        { get { return new MojEnumVar<string>("LanguageFile", StorageType.SystemVar); } }
        public static MojEnumVar<string> SkinFile
        { get { return new MojEnumVar<string>("SkinFile", StorageType.SystemVar); } }
        public static MojEnumVar<string> DefaultCoverPath
        { get { return new MojEnumVar<string>("DefaultCover", StorageType.SystemVar); } }
        public static MojEnumVar<string> MoviesExtensions
        { get { return new MojEnumVar<string>("MojMovieFilesExtensions", StorageType.SystemVar); } }
        public static MojEnumVar<string> SubsExtensions
        { get { return new MojEnumVar<string>("MojSubFilesExtensions", StorageType.SystemVar); } }
        public static MojEnumVar<string> ImagesExtensions
        { get { return new MojEnumVar<string>("MojImageFilesExtensions", StorageType.SystemVar); } }

        public static MojEnumVar<bool> PluginsDefaultState
        { get { return new MojEnumVar<bool>("PluginsDefaultState", StorageType.SystemVar); } }
        public static MojEnumVar<bool> DeveloperMode
        { get { return new MojEnumVar<bool>("MojStartInDevMode", StorageType.SystemVar); } }
        public static MojEnumVar<bool> ShowStyleInspector
        { get { return new MojEnumVar<bool>("Moj_dev_ShowStyleInspector", StorageType.SystemVar); } }

        public static void Inicialize()
        {
            LanguageFile.Create("DEFAULT");
            SkinFile.Create("MoJBlack");
            DefaultCoverPath.Create(Path.Combine(MojFoldersEnum.ImagesFolder.Value(), "DefaultPoster.jpg"));

            MoviesExtensions.Create(".mkv;.avi;.mp4");
            SubsExtensions.Create(".srt");
            ImagesExtensions.Create(".jpg;.jpeg;.png");

            PluginsDefaultState.Create(true);
            DeveloperMode.Create(false);
            ShowStyleInspector.Create(true);
        }
    }
    public class MojStatusVarsEnum
    {
        public static MojEnumVar<bool> IsSettingsWindowVisible
        { get { return new MojEnumVar<bool>("sv_IsSettingsWindowVisible", StorageType.StatusVar); } }
        public static MojEnumVar<bool> IsSearchMovieDBWindowVisible
        { get { return new MojEnumVar<bool>("sv_IsSearchMovieDBWindowVisible", StorageType.StatusVar); } }
        public static MojEnumVar<bool> IsMovieSelected
        { get { return new MojEnumVar<bool>("sv_IsMovieSelected", StorageType.StatusVar); } }
        public static MojEnumVar<bool> IsFileSelected
        { get { return new MojEnumVar<bool>("sv_IsFileSelected", StorageType.StatusVar); } }
        public static MojEnumVar<bool> ShowSystemErrorReporter
        { get { return new MojEnumVar<bool>("ShowSystemErrorReporter", StorageType.StatusVar); } }
        public static MojEnumVar<bool> IsMovieDetailsWindowVisible
        { get { return new MojEnumVar<bool>("sv_isWinMovieDetailsVisible", StorageType.StatusVar); } }

        public static MojEnumVar<string> SelectedMovie
        { get { return new MojEnumVar<string>("sv_SelectedMovie", StorageType.StatusVar); } }
        public static MojEnumVar<string> SelectedFile
        { get { return new MojEnumVar<string>("sv_SelectedFile", StorageType.StatusVar); } }

        public static MojEnumVar<IPanel> DisplayPanel
        { get { return new MojEnumVar<IPanel>("sv_DisplayPanel", StorageType.StatusVar); } }
        public static MojEnumVar<IMetaData> MetaDataControl
        { get { return new MojEnumVar<IMetaData>("sv_MetaDataControl", StorageType.StatusVar); } }

        public static void Inicialize()
        {
            IsSettingsWindowVisible.Create(false);
            IsSearchMovieDBWindowVisible.Create(false);
            IsMovieSelected.Create(false);
            IsFileSelected.Create(false);
            ShowSystemErrorReporter.Create(true);
            IsMovieDetailsWindowVisible.Create(false);

            SelectedFile.Create("NULL");
            SelectedMovie.Create("NULL");
            DisplayPanel.Create(null);
            MetaDataControl.Create(null);
        }
    }

    public class MojEnumVar<T> where T : IEquatable<T>
    {
        private string _var;
        private StorageType _store;

        public MojEnumVar(string VarKey, StorageType Store)
        {
            _var = VarKey;
            _store = Store;
        }
        public MojEnumVar()
        { 
            _var = null;
            _store = StorageType.NULL;
        }

        public static implicit operator string(MojEnumVar<T> var)
        { return var._var; }

        public T Value()
        {
            switch (_store)
            {
                case StorageType.NULL:
                    throw new Exception("Store not implemented");

                case StorageType.SystemVar:
                    return MojHost.GetHost.MoJSystem.SystemVars.Get<T>(_var);

                case StorageType.UserVar:
                    return MojHost.GetHost.MoJSystem.UserVars.Get<T>(_var);

                case StorageType.DynamicVar:
                    return MojHost.GetHost.MoJSystem.DynamicVars.Get<T>(_var);

                case StorageType.StatusVar:
                    return MojHost.GetHost.MoJSystem.StatusVars.Get<T>(_var);
            }

            throw new Exception("Store not implemented");
        }
        public void Set(T VarValue)
        {
            T tempValue = this.Value();

            switch (_store)
            {
                case StorageType.NULL:
                    return;

                case StorageType.SystemVar:
                    MojHost.GetHost.MoJSystem.SystemVars.Set<T>(_var, VarValue);
                    break;

                case StorageType.UserVar:
                    MojHost.GetHost.MoJSystem.UserVars.Set<T>(_var, VarValue);
                    break;

                case StorageType.DynamicVar:
                    MojHost.GetHost.MoJSystem.DynamicVars.Set(_var, VarValue);
                    break;

                case StorageType.StatusVar:
                    MojHost.GetHost.MoJSystem.StatusVars.Set(_var, VarValue);
                    break;

                default:
                    return;
            }
        }
        public bool Create(T VarValue)
        {
            IHost moj = MojHost.GetHost;

            switch (_store)
            {
                case StorageType.NULL:
                    return false;

                case StorageType.SystemVar:
                    MojHost.GetHost.MoJSystem.SystemVars.Create<T>(_var, VarValue);
                    return true;

                case StorageType.UserVar:
                    MojHost.GetHost.MoJSystem.UserVars.Create<T>(_var, VarValue);
                    break;

                case StorageType.DynamicVar:
                    if (moj.MoJSystem.DynamicVars.Exist(_var))
                        return false;

                    MojHost.GetHost.MoJSystem.DynamicVars.Set(_var, VarValue);
                    return true;

                case StorageType.StatusVar:
                    if (moj.MoJSystem.StatusVars.Exist(_var))
                        return false;

                    MojHost.GetHost.MoJSystem.StatusVars.Set(_var, VarValue);
                    return true;
            }

            return false;
        }
    }
    public enum StorageType
    {
        SystemVar,
        UserVar,
        DynamicVar,
        StatusVar,
        NULL,
    }
}
